package com.ruoyi.project.module.lys;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.CxcyBaseinfo;
import com.ruoyi.project.domain.CxcyDemand;
import com.ruoyi.project.service.ICxcyBaseinfoService;
import com.ruoyi.project.service.ICxcyDemandService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 企业需求Controller
 * 
 * @author yinhe
 * @date 2023-09-19
 */
@RestController
@RequestMapping("/lysdemand")
public class LysCxcyDemandController extends BaseController
{
    @Autowired
    private ICxcyDemandService cxcyDemandService;
    @Autowired
    private ICxcyBaseinfoService cxcyBaseinfoService;

    /**
     * 审核人查询企业需求列表
     */
//    @RequiresPermissions("project:demand:list")
    @GetMapping("/list")
    public TableDataInfo list(CxcyDemand cxcyDemand)
    {

        startPage();
        List<CxcyDemand> list = cxcyDemandService.selectCxcyDemandList(cxcyDemand);
        return getDataTable(list);
    }
    /**
     * 填报人查询企业需求列表
     */
//    @RequiresPermissions("project:demand:list")
    @GetMapping("/tianbaolist")
    public TableDataInfo tianbaolist(CxcyDemand cxcyDemand)
    {
        //获取登录人的userId
        LoginUser loginUser=getLoginUser();
        SysUser sysUser=loginUser.getSysUser();
        startPage();
        //将用户的userid作为firstid的查询条件进行反显
        cxcyDemand.setFirstId(sysUser.getUserId());
        List<CxcyDemand> list = cxcyDemandService.selectCxcyDemandList(cxcyDemand);
        return getDataTable(list);
    }

    /**
     * 导出企业需求列表
     */
//    @RequiresPermissions("project:demand:export")
    @Log(title = "企业需求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CxcyDemand cxcyDemand)
    {
        List<CxcyDemand> list = cxcyDemandService.selectCxcyDemandList(cxcyDemand);
        ExcelUtil<CxcyDemand> util = new ExcelUtil<CxcyDemand>(CxcyDemand.class);
        util.exportExcel(response, list, "企业需求数据");
    }

    /**
     * 获取企业需求详细信息
     */
//    @RequiresPermissions("project:demand:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cxcyDemandService.selectCxcyDemandById(id));
    }

    /**
     * 新增企业需求
     */
//    @RequiresPermissions("project:demand:add")
    @Log(title = "企业需求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CxcyDemand cxcyDemand)
    {
        //获取登录人的userId
        LoginUser loginUser=getLoginUser();
        SysUser sysUser=loginUser.getSysUser();
        CxcyBaseinfo cxcyBaseinfo=new CxcyBaseinfo();
        System.out.println("userId"+sysUser.getUserId());
        //在baseinfo中通过userId查到对应的信息
        cxcyBaseinfo.setUserId(sysUser.getUserId());
        List<CxcyBaseinfo> list = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
        System.out.println("1"+cxcyDemand);
        if(list.size()==0){
            //传入登录用户的姓名和id
            cxcyDemand.setFirstName(sysUser.getNickName());
            cxcyDemand.setFirstId(sysUser.getUserId());
            return toAjax(cxcyDemandService.insertCxcyDemand(cxcyDemand));
        }
        else {
            //传入登录用户的姓名和id
            cxcyDemand.setFirstName(sysUser.getNickName());
            cxcyDemand.setFirstId(sysUser.getUserId());
            for(CxcyBaseinfo baseinfo: list )
            {
                //注入企业名称
                cxcyDemand.setEnterpriseName(baseinfo.getEnterpriseName());
                //注入法人身份证号
                cxcyDemand.setCorporateNumber(baseinfo.getCorporateNumber());
                //注入法人邮箱
                cxcyDemand.setCorporateEmail(baseinfo.getCorporateEmail());
                //注入法人电话
                cxcyDemand.setCorporatePhone(baseinfo.getCorporatePhone());
                //注入法人身份证号
                cxcyDemand.setCorporateNumber(baseinfo.getCorporateNumber());
                //注入企业营业执照
                cxcyDemand.setBusinessLicense(baseinfo.getBusinessLicense());
                //注入企业地址
                cxcyDemand.setEnterpriseAddress(baseinfo.getEnterpriseAddress());
            }
        }
        return toAjax(cxcyDemandService.insertCxcyDemand(cxcyDemand));
    }

    /**
     * 审核
     */
//    @RequiresPermissions("project:demand:edit")
    @Log(title = "企业需求", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CxcyDemand cxcyDemand)
    {
        //获取登陆人信息
        LoginUser loginUser=getLoginUser();
        SysUser sysUser=loginUser.getSysUser();
        //将登陆人信息存到配包表指定字段
        cxcyDemand.setAuditName(sysUser.getNickName());
        cxcyDemand.setAuditTime(DateUtils.getNowDate());
        cxcyDemand.setAuditNumber(Long.valueOf(sysUser.getUserId()));
        return toAjax(cxcyDemandService.updateCxcyDemand(cxcyDemand));
    }
    /**
     * 修改
     */
//    @RequiresPermissions("project:demand:edit")
    @Log(title = "企业需求", businessType = BusinessType.UPDATE)
    @PutMapping("/xiugai")
    public AjaxResult edit1(@RequestBody CxcyDemand cxcyDemand)
    {
        return toAjax(cxcyDemandService.updateCxcyDemand(cxcyDemand));
    }

    /**
     * 删除企业需求
     */
//    @RequiresPermissions("project:demand:remove")
    @Log(title = "企业需求", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cxcyDemandService.deleteCxcyDemandByIds(ids));
    }
}
